﻿#include "Matrix.h"
#include "Input.h"
#include <vector>
using namespace std;

void menu(vector<Matrix>& mats);
void input(vector<Matrix>& mats);
void input_ide(vector<Matrix>& mats);

int output_all(const vector<Matrix>& mats);
Matrix* get_matrix(vector<Matrix>& mats, int index);
void m2_m_menu(vector<Matrix>& mats, Matrix(*op)(Matrix, Matrix));
void m_m_menu(vector<Matrix>& mats, Matrix(*op)(Matrix));
void md_m_menu(vector<Matrix>& mats, Matrix(*op)(Matrix, double));
void m_d_menu(vector<Matrix>& mats, double (*op)(Matrix));
void menu3(vector<Matrix>& mats) { m2_m_menu(mats, Matrix::add_matrix); }
void menu4(vector<Matrix>& mats) { m2_m_menu(mats, Matrix::sub_matrix); }
void menu5(vector<Matrix>& mats) { md_m_menu(mats, Matrix::mul_num); }
void menu6(vector<Matrix>& mats) { m2_m_menu(mats, Matrix::mul_matrix); }
void menu7(vector<Matrix>& mats) { m_m_menu(mats, Matrix::trans_matrix); }
void menu8(vector<Matrix>& mats) { m_m_menu(mats, Matrix::inv_matrix); }
void menu9(vector<Matrix>& mats) { m_d_menu(mats, Matrix::det_matrix); }
void menu10(vector<Matrix>& mats);

int main() {
	vector<Matrix> mats;
	menu(mats);
	return 0;
}

void menu(vector<Matrix>& mats) {
	for (;;) {
		system("cls");
		cout << "矩阵运算" << endl;
		cout << "--------------------" << endl;
		cout << "1. 输入矩阵" << endl;
		cout << "2. 输出矩阵" << endl;
		cout << "3. 求两个矩阵加" << endl;
		cout << "4. 求两个矩阵减" << endl;
		cout << "5. 求矩阵乘常数" << endl;
		cout << "6. 求矩阵乘矩阵" << endl;
		cout << "7. 求矩阵的转置" << endl;
		cout << "8. 求矩阵的逆" << endl;
		cout << "9. 求矩阵的行列式" << endl;
		cout << "10. 删除矩阵" << endl;
		cout << "11. 输入单位矩阵" << endl;
		cout << "0. 退出" << endl;
		cout << "--------------------" << endl;
		cout << "输入选项：";

		int op = nextInt();

		try {
			switch (op) {
			case 0: return;
			case 1: input(mats); break;
			case 2: output_all(mats); break;
			case 3: menu3(mats); break;
			case 4: menu4(mats); break;
			case 5: menu5(mats); break;
			case 6: menu6(mats); break;
			case 7: menu7(mats); break;
			case 8: menu8(mats); break;
			case 9: menu9(mats); break;
			case 10: menu10(mats); break;
			case 11: input_ide(mats); break;
			}
		}
		catch (const exception& e) {
			cout << "操作失败: " << e.what() << endl;
		}
		system("pause");
	}
}

void input(vector<Matrix>& mats) {
	cout << "输入要添加的矩阵数量：" << endl;
	int n = nextInt();

	for (int i = 0; i < n; i++) {
		cout << "输入第 " << mats.size() << " 个矩阵" << endl;

		cout << "行数：" << endl;
		int r = nextInt();
		cout << "列数：" << endl;
		int c = nextInt();

		Matrix mat(r, c);
		input_matrix(mat);
		mats.push_back(mat);
	}
}
void input_ide(vector<Matrix>& mats) {
	cout << "输入要添加的单位矩阵数量：" << endl;

	int n = nextInt();
	for (int i = 0; i < n; i++) {
		cout << "输入第 " << mats.size() << " 个单位矩阵的大小" << endl;
		int size = nextInt();
		mats.push_back(IdentityMatrix(size));
	}
	

	}
	int output_all(const vector<Matrix>&mats) {
		if (mats.empty()) {
			cout << "无矩阵" << endl;
			return 0;
		}
		cout << "输出矩阵：" << endl;
		cout << "----------" << endl;
		for (size_t i = 0; i < mats.size(); i++) {
			cout << "矩阵 " << i << ":" << endl;
			mats[i].output_matrix();
			cout << "----------" << endl;
		}
		return mats.size();
	}

	Matrix* get_matrix(vector<Matrix>&mats, int index) {
		if (index < 0 || index >= static_cast<int>(mats.size())) {
			return nullptr;
		}
		return &mats[index];
	}

	void m2_m_menu(vector<Matrix>&mats, Matrix(*op)(Matrix, Matrix)) {
		int len = output_all(mats);
		if (len == 0) return;

		cout << "输入第一个矩阵的索引：" << endl;
		int i = nextInt();
		if (i < 0 || i >= len) {
			cout << "索引超出范围！" << endl;
			return;
		}
		cout << "输入第二个矩阵的索引：" << endl;
		int j = nextInt();
		if (j < 0 || j >= len) {
			cout << "索引超出范围！" << endl;
			return;
		}

		Matrix* a = get_matrix(mats, i);
		Matrix* b = get_matrix(mats, j);
		if (!a || !b) {
			cout << "无效的矩阵" << endl;
			return;
		}

		Matrix ret = op(*a, *b);
		cout << "结果矩阵:" << endl;
		ret.output_matrix();
	}

	void m_m_menu(vector<Matrix>&mats, Matrix(*op)(Matrix)) {
		int len = output_all(mats);
		if (len == 0) return;

		cout << "输入矩阵的索引：" << endl;
		int i = nextInt();
		if (i < 0 || i >= len) {
			cout << "输入的索引无效！" << endl;
			return;
		}

		Matrix* a = get_matrix(mats, i);
		if (!a) {
			cout << "无效的矩阵" << endl;
			return;
		}

		Matrix ret = op(*a);
		cout << "结果矩阵:" << endl;
		ret.output_matrix();
	}

	void md_m_menu(vector<Matrix>&mats, Matrix(*op)(Matrix, double)) {
		int len = output_all(mats);
		if (len == 0) return;

		cout << "输入矩阵的索引：" << endl;
		int i = nextInt();
		if (i < 0 || i >= len) {
			cout << "索引无效！" << endl;
			return;
		}
		cout << "输入常数：" << endl;
		double num = nextDouble();

		Matrix* a = get_matrix(mats, i);
		if (!a) {
			cout << "无效的矩阵" << endl;
			return;
		}

		Matrix ret = op(*a, num);
		cout << "结果矩阵:" << endl;
		ret.output_matrix();
	}

	void m_d_menu(vector<Matrix>&mats, double (*op)(Matrix)) {
		int len = output_all(mats);
		if (len == 0) return;

		cout << "输入矩阵的索引：" << endl;
		int i = nextInt();
		if (i < 0 || i >= len) {
			cout << "输入的索引无效！" << endl;
			return;
		}

		Matrix* a = get_matrix(mats, i);
		if (!a) {
			cout << "无效的矩阵" << endl;
			return;
		}

		double ret = op(*a);
		cout << "结果：" << ret << endl;
	}

	void menu10(vector<Matrix>&mats) {
		system("cls");
		int len = output_all(mats);
		if (len <= 0) {
			cout << "无矩阵" << endl;
			return;
		}

		int idx;
		while (true) {
			cout << "输入删除的矩阵的索引：" << endl;
			idx = nextInt();
			if (idx < 0 || idx >= len) {
				cout << "索引无效！" << endl;
				continue;
			}
			break;
		}

		mats.erase(mats.begin() + idx);
		cout << "删除完成。" << endl;
		output_all(mats);
	}